package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //查询所有
    @RequestMapping("/list")
    @RequiresPermissions(value = {"department:list", "部门列表"},logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){

        PageInfo pageInfo = departmentService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/department/list";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"department:delete", "部门删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        departmentService.deleteByPrimaryKey(id);
        return new JsonResult();
    }
   /* @RequestMapping("/input")
    @ResponseBody
    public JsonResult input(Long id, Model model){
        try {
            if (id != null) {
                Department department = departmentService.get(id);
                model.addAttribute("department",department);
            }
        }catch (Exception e){
            return new JsonResult(false,"保存失败,请重新处理!");
        }
        return null;
    }*/
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"department:saveOrupdate", "部门新增/编辑"},logical = Logical.OR)
    public JsonResult saveOrupdate(Department department){
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            }else{
                departmentService.updateByPrimaryKey(department);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败,请重新操作!");
        }
    }
}
